feed_the_beastfandomcom-20200223-history
Basic Lua
Getting Started This is a guide which attempts to impart the basics of programming to someone who is not familiar with programming languages or used a computer craft turtle. The first thing you need to know when programming in computer craft is how to use the text interface in a turtle. Running a Program: To run a program type the name of the program you want to run and any input the program will need if any separated by a single space. For example, tunnel 100 will make a mining turtle dig a 2 high by 3 wide by 100 long tunnel. Creating a program: To create a program of your own type "edit " The name of a program must be a single string of charecters. Multiple words may be placed together if needed but it is suggested that the first letter of each subsiquent word be capatalized. This is often known as camle case in programing. Save your program: To save your program you will need to make a disk drive and a floppy disk. Place your disk drive next to your computer, and put in your floppy. Then type copy name of the program disk Access a program on a floppy disk: To access a program on a disk you will need to first change the directory your in to the disks. To do so type cd disk Now all you have to do is type the name of your program and any inputs it needs. Alternatively, you can type "disk/". First Program The most basic program you can create with a computer is known as the Hello world program. This program makes the turtle speak in the screen "Hello world". For now we will create a program which does something slightly more useful. This program will make a mining turtle branch mine for you. First type edit mine. You will now see a blank window. holding control allows you to access the menu, which lets you save, exit, or print. Using The Turtle API To make your turtle do something you will need to call a function from its Api. A function is basically a bit of code which can be run multiple times. I will get into more about how to make one later, but for now that is all you need to know. The first thing you will want your turtle to do as it starts mining is to dig the block infront of it. To make it do this, type turtle.dig() We also may want to have the turtle dig above and bellow it. Type turtle.digUp() to dig above. then type turtle.digDown() to dig bellow. Next we will want our turtle to move forward. To make your turtle move forward, type turtle.forward() At this point your code should look like this: turtle.dig() turtle.digUp() turtle.digDown() turtle.forward() If you save and run mine, your turtle will dig forward, up, down then move forward. Conditional Statements As we have it right now, the turtle may waste allot of time, since it digs without checking if there is something to dig. To make the turtle check to see if there's a block in-front of it, you will need to use an if statement. The syntax for an if statement is as follows: if condition then : to run when the condition is true end For our program, edit mine add an if statement using the turtle.detect() function in the condition. your code should look like this: if turtle.detect() then : turtle.dig() turtle.forward() end We should do the same to check if there is a block above using turtle.detectUp() and bellow with turtle.detectDown() After you add these if statements our code should look like this: if turtle.detect() : turtle.dig() end if turtle.detectUp() then : turtle.digUp() end if turtle.detectDown() then : turtle.digDown() end turtle.forward() variables Before we talk about the loops, we should spend a moment to learn about variables. A variable stores information specified by the programmer. This information may be a numeric value, a Boolean value(true or false) or a string(a collection) of characters(letters, numbers, symbols). A variable is declared(when you tell the computer that it exists, its name, and value) using this syntax: single word name that starts with an letter = value or contents of variable For example if I wanted a variable to hold the value of how far I want my turtle to dig, I was make a variable like this: length = 100 It is usually helpful for variables to be placed at the top of your code, unless they are temporary. A temporary variable(known as local) only exists within the body of a specific element of code. for instance if you made your variable in an if statement, it probably would not be defined after the end. You should now add length = 100 above your code in mine, so that it looks like this: length = 100 if turtle.detect() turtle.dig() end if turtle.detectUp() then : turtle.digUp() end if turtle.detectDown() then : turtle.digDown() end turtle.forward() Loops Our turtle can now dig a block in front of it if there is one and then move forward once, but as it is we would have to rewrite those two commands over and over to get it to repeat. Instead of doing this we can use a loop to have the computer repeat those lines of code as many times as we need.There are at least two kinds of loops in lua. We want the turtle to move and mine a predefined number of times ahead so we want to use a counting loop, which is known as a for loop. A for loop's syntax is as follows: for variable=value , value , to count by do : to loop end for our for loop, we will want to have it start at 1 and end at length. The following is an example of how to do this: for i=1, length, 1 do to repeat end This loop will first check to see if the counter is equal to the end value, and if it is not then it adds one and does whatever is within the body of the loop(between the do and end) At this point your code should look something like this: length = 100 for i=1,length,1 do if turtle.detect() then turtle.dig() end if turtle.detectUp() then : turtle.digUp() end if turtle.detectDown() then : turtle.digDown() end turtle.forward() end When we run the program as is, you will find that there is a problem which will make the end result unpredictable. If the turtle runs into falling sand or gravel, its likely that it will cause the turtle to behave ina way we dont want, becuase the tunnel will be shorter than we planned. To account for this we can replace the if conditions with another kind of loop, so that before moving ahead the turtle will check if theres a new obsitcle. This loop is called a while loop. This is a while loops syntax: while statement do to loop end For an example of how we can use a while loop , we can replace the if condition in our program with a while loop. We will also want to make the turtle wait a second after digging to account for the time it takes for a block to fall. To do so we use the sleep(1) function. Your while loop should look like this: while turtle.detect() do turtle.dig() sleep(1) end You should go ahead and also replace the if condition for checking if theres a block above the turtle with a while loop, but it is not nessisary to do so with the down. Functions Sofar we have used a few different functions in our code without really knowing what they are. A function is a segment of code which you can call from somewhere else in our program as many times as you want. The following is the syntax for a function: function of function in a single word() code end Using functions in your code has a few benifits. The first is that you can make a more complicated action then easily repeat it. In our program we will create a function called dig, which will allow you to perform the dig forward, dig up, and dig down from our current code by calling the function instead of repeating all that code.